package org.ithasu.filpilote.app;

import org.ithasu.filpilote.data.ZoneModel;
import org.ithasu.filpilote.data.entities.Program;
import org.ithasu.filpilote.data.entities.Rule;
import org.ithasu.filpilote.data.entities.Wire;
import org.ithasu.filpilote.data.entities.Zone;
import org.ithasu.tools.CollectionTools;
import org.ithasu.tools.Console;
import org.ithasu.tools.TimeTools;

public class Daemon {

	public static void main(String[] args) {
		String filename = "/home/matthieu/wires.json";
		ZoneModel model = new ZoneModel();
		if (!model.load(filename)) {
			Console.error("Failed to load: " + filename);
		} else {
			Console.message("Found " + CollectionTools.size(model.getWires()) + " wire(s).");
			for (Zone z : model.getZones()) {
				Console.message("Zone : " + z.getName() + " (" + z.getId() + ")");
				for (Wire w : model.getZoneWires(z)) {
					Console.message("- " + w.getId() + ": " + w.getName() + " (" + w.getDefaultMode() + ")");
					for (Rule r : model.getWireRules(w)) {
						Console.message("  - " + r.toString());
					}
				}
			}
			Console.message(CollectionTools.size(model.getPrograms()) + " programs available.");
			for (Program p : model.getPrograms()) {
				Console.message("- " + p.getId() + ": " + p.getName() + " - " + CollectionTools.size(p.getRules()) + " rules");
			}

			Console.message("Default program : " + model.getCurrentProgram().get().getName());
		}

		Console.message("");

		long midnight = TimeTools.getLocalTime();
		midnight -= TimeTools.getMinutesFromMidnight(midnight) * 60 * 1000;
		for (int i = 0; i < 7 * 24 * 60; i+=15) {
			long time = midnight + ((long) i) * 60 * 1000;
			String text = TimeTools.day(TimeTools.getDayOfWeek(time)) + " "
					+ TimeTools.minutes(TimeTools.getMinutesFromMidnight(time)) + " :";
			for (Wire w : model.getWires()) {
				text += "\t" + String.valueOf(model.getWireMode(w, time));
			}
			Console.message(text);
		}
	}
}
